﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using WS.EKA.Business.Service;
using WS.EKA.Business.Model;
using WS.EKA.Utility;
using System.Threading.Tasks;
using WS.EKA.UI.Business;

namespace WS.EKA.Web.UI.Service
{
    /// <summary>
    /// Summary description for EventService
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class EventService : System.Web.Services.WebService
    {
        [WebMethod]
        public void SaveEvent(int Event_Id, string Event_Level, string Ticket_Id, string Event_Source, string Host_Id,
                DateTime Begin_Time, string Event_Desc, string Status, string App_Name, string App_Code)
        {
            Logger.Log("EventService Begining...");

            try
            {
                IService<TB_EVENT_TEMP> service = ServiceFactory.CreateService<TB_EVENT_TEMP>();

                TB_EVENT_TEMP data = new TB_EVENT_TEMP()
                {
                    Event_Id = Event_Id,
                    Event_Level = Event_Level,
                    Ticket_Id = Ticket_Id,
                    Event_Source = Event_Source,
                    Host_Id = Host_Id,
                    Begin_Time = Begin_Time,
                    Event_Desc = Event_Desc,
                    Status = Status,
                    App_Name = App_Name,
                    App_Code = App_Code
                };
                var list = new List<TB_EVENT_TEMP>();
                list.Add(data);
                service.ImportData(list);
            }
            catch (Exception ex)
            {
                Logger.Log("EventService Error.");
                Logger.Error("EventService Error.", ex);
                throw;
            }

            Logger.Log("EventService End.");
        }

        [WebMethod]
        public void SaveEventByString(string data)
        {
            Logger.Log("EventService Begining...");

            try
            {
                IService service = ServiceFactory.CreateService(ServiceType.EVENT.ToString());

                service.ImportData(data);
            }
            catch (Exception ex)
            {
                Logger.Log("EventService Error.");
                Logger.Log("Data:" + data);
                Logger.Error("EventService Error.", ex);
                Logger.Error("Data:" + data,null);
                throw;
            }

            PushToClient();
            Logger.Log("EventService End.");
        }

      
        private void PushToClient()
        {
            Task.Factory.StartNew(() =>
            {
                ClientDataSourceBridge.PushEventToClient();

            }).ContinueWith(t =>
            {
                foreach (var inner in t.Exception.InnerExceptions)
                {
                    Logger.Error("Incident push Error.", inner);
                }
            }, TaskContinuationOptions.OnlyOnFaulted);
        }
    }
}
